package com.lechi.web.controller;

import com.lechi.web.common.annotation.Log;
import com.lechi.web.common.core.controller.BaseController;
import com.lechi.web.common.core.domain.AjaxResult;
import com.lechi.web.common.core.page.TableDataInfo;
import com.lechi.web.common.enums.BusinessType;
import com.lechi.web.common.exception.ExceptionUtil;
import com.lechi.web.constants.Constants;
import com.lechi.web.service.IIotIndustrySensorRelService;
import com.lechi.web.service.IIotIndustryService;
import com.lechi.web.system.domain.IotIndustry;
import com.lechi.web.system.domain.IotIndustrySensorRel;
import com.lechi.web.vo.IotIndustryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 行业分类Controller
 *
 * @author yangguang
 * @date 2024 -05-17
 */
@RestController
@RequestMapping("/iot/industry")
@RequiredArgsConstructor
public class IotIndustryController extends BaseController {

    private final IIotIndustryService iotIndustryService;

    private final IIotIndustrySensorRelService iotIndustrySensorRelService;

    /**
     * 查询行业分类列表
     *
     * @param iotIndustry the iot industry
     * @return the table data info
     */
    @GetMapping("/list")
    //    @PreAuthorize("@ss.hasPermi('iot:industry:query')")
    public TableDataInfo list(IotIndustry iotIndustry) {
        startPage();
        List<IotIndustry> list = iotIndustryService.selectIotIndustryList(iotIndustry);
        return getDataTable(list);
    }

    /**
     * 获取行业分类详细信息
     *
     * @param id the id
     * @return the info
     */
    @GetMapping(value = "/{id}")
    //    @PreAuthorize("@ss.hasPermi('iot:industry:query')")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        IotIndustry iotIndustry = Optional.ofNullable(iotIndustryService.selectIotIndustryById(id)).orElseThrow(ExceptionUtil.notExitsErrorException);

        IotIndustryVo iotIndustryVo = new IotIndustryVo();
        BeanUtils.copyProperties(iotIndustry, iotIndustryVo);

        // 获取行业下面关联的要素
        IotIndustrySensorRel iotDeviceSensorRelBo = new IotIndustrySensorRel();
        iotDeviceSensorRelBo.setIndustryId(id);
        List<IotIndustrySensorRel> deviceSensorList = iotIndustrySensorRelService.selectIotIndustrySensorRelList(iotDeviceSensorRelBo);

        iotIndustryVo.setIndustrySensorList(deviceSensorList);
        return success(iotIndustryVo);
    }

    /**
     * 新增行业分类
     *
     * @param iotIndustryVo the iot industry vo
     * @return the ajax result
     */
    @Transactional
    @PostMapping(value = "/add")
    //    @PreAuthorize("@ss.hasPermi('iot:industry:add')")
    public AjaxResult add(@RequestBody IotIndustryVo iotIndustryVo) {
        IotIndustry iotIndustry = new IotIndustry();
        BeanUtils.copyProperties(iotIndustryVo, iotIndustry);
        iotIndustryService.insertIotIndustry(iotIndustry);
        if (iotIndustryVo.getIndustrySensorList() != null) {
            iotIndustryVo.getIndustrySensorList().forEach(iotDeviceSensorRelVo -> {
                IotIndustrySensorRel iotDeviceSensorRelBo = new IotIndustrySensorRel();
                BeanUtils.copyProperties(iotDeviceSensorRelVo, iotDeviceSensorRelBo);
                iotDeviceSensorRelBo.setIndustryId(iotIndustry.getId());
                iotIndustrySensorRelService.insertIotIndustrySensorRel(iotDeviceSensorRelBo);
            });
        }
        return AjaxResult.success();
    }

    /**
     * 修改行业分类
     *
     * @param iotIndustryVo the iot industry vo
     * @return the ajax result
     */
    @Transactional
    @PostMapping("/edit")
    @Log(title = "修改行业分类", businessType = BusinessType.UPDATE)
    //    @PreAuthorize("@ss.hasPermi('iot:industry:edit')")
    public AjaxResult edit(@RequestBody IotIndustryVo iotIndustryVo) {
        IotIndustry iotIndustry = new IotIndustry();
        BeanUtils.copyProperties(iotIndustryVo, iotIndustry);
        iotIndustryService.updateIotIndustry(iotIndustry);
        iotIndustryVo.getIndustrySensorList().forEach(industrySensorRelVo -> {
            IotIndustrySensorRel iotIndustrySensorRel = new IotIndustrySensorRel();
            BeanUtils.copyProperties(industrySensorRelVo, iotIndustrySensorRel);

            if (Objects.equals(industrySensorRelVo.getId(), Constants.ADD_FLAG)) {
                iotIndustrySensorRelService.insertIotIndustrySensorRel(iotIndustrySensorRel);
            } else {
                iotIndustrySensorRelService.updateIotIndustrySensorRel(iotIndustrySensorRel);
            }
        });
        return AjaxResult.success();
    }

    /**
     * 删除行业分类
     *
     * @param ids the ids
     * @return the ajax result
     */
    @Transactional
    @DeleteMapping("/{ids}")
    @Log(title = "删除行业分类", businessType = BusinessType.UPDATE)
    //    @PreAuthorize("@ss.hasPermi('iot:industry:remove')")
    public AjaxResult remove(@PathVariable Long[] ids) {
        Arrays.stream(ids).forEach(id -> {
            IotIndustrySensorRel iotIndustrySensorRel = new IotIndustrySensorRel();
            iotIndustrySensorRel.setIndustryId(id);
            iotIndustrySensorRelService.deleteIotIndustrySensorRelByIndustryId(iotIndustrySensorRel);
        });
        iotIndustryService.deleteIotIndustryByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 删除设备关联观测要素关系
     *
     * @param sensorAliasIds the sensor alias ids
     * @return the ajax result
     */
    @DeleteMapping("/sensor/{sensorAliasIds}")
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public AjaxResult sensorremove(@PathVariable Long[] sensorAliasIds) {
        Arrays.asList(sensorAliasIds).forEach(sensorAliasId -> {
            iotIndustrySensorRelService.deleteIotIndustrySensorRelByIds(sensorAliasIds);
        });
        return AjaxResult.success();
    }
}
